// https://web.dev/articles/fcp?hl=zh-cn
// import { lazyReportBatch } from '../report'
/**
 * 上报数据 格式
{
    duration: 0,
    entryType: "paint",
    name: "first-contentful-paint",
    startTime: 459, // fcp 时间
	type: 'performance', // 上报大类型
	subType: 'first-contentful-paint', // 上报子类型
	pageUrl: window.location.href // 页面地址
}
 */
export const observerFCP = () => {
	const entryHandler = (entryList) => {
		for (const entry of entryList.getEntriesByName('first-contentful-paint')) {
			if (entry.name === 'first-contentful-paint') {
				// 断开观察者：断开observer的连接，防止继续观察性能事件
				observer.disconnect()
				console.log('entry', entry)
				const json = entry.toJSON()
				console.log('json', json)

				// 构建上报数据
				const reportData = {
					...json,
					type: 'performance',
					subType: entry.name,
					pageUrl: window.location.href
				}
				console.log('reportData', reportData)
				// TODO: 数据上报
			}
		}
	}
	const observer = new PerformanceObserver(entryHandler)
	observer.observe({ type: 'paint', buffered: true })
}
